<html>
<head><meta charset="utf-8"><title>try else · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html">try else</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213980807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213980807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213980807">(Oct 20 2020 at 21:18)</a>:</h4>
<p>Just spitballing here: It would be nice to have some kind of syntax sugar like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_stuff</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// If `do_stuff()` returned `Err` or `None`.</span>
<span class="w">    </span><span class="n">do_other_stuff</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>This would desugar into this for <code>Result</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">do_stuff</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">do_other_stuff</span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>and this for <code>Option</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">do_stuff</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">do_other_stuff</span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>What do you think?</p>



<a name="213980931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213980931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213980931">(Oct 20 2020 at 21:20)</a>:</h4>
<p>It would be nice specifically because then the two branches would be symmetrical, as opposed to with <code>match</code> or <code>if let</code></p>



<a name="213981013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213981013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213981013">(Oct 20 2020 at 21:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213980931">said</a>:</p>
<blockquote>
<p>It would be nice specifically because then the two branches would be symmetrical, as opposed to with <code>match</code> or <code>if let</code></p>
</blockquote>
<p>I don't quite follow this bit</p>



<a name="213981042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213981042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213981042">(Oct 20 2020 at 21:21)</a>:</h4>
<p>what's the advantage over <code>let res = try { do_stuff(); } match res { ... }</code>?</p>



<a name="213981103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213981103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213981103">(Oct 20 2020 at 21:22)</a>:</h4>
<p>that would let you handle <code>Err(e)</code> without bikeshedding about how to bind <code>e</code>, too</p>



<a name="213984927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213984927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213984927">(Oct 20 2020 at 21:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213981042">said</a>:</p>
<blockquote>
<p>what's the advantage over <code>let res = try { do_stuff(); } match res { ... }</code>?</p>
</blockquote>
<p>Not sure what you mean. Did you mean this?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">do_stuff</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985103">(Oct 20 2020 at 22:01)</a>:</h4>
<p>I forgot the semicolon, sorry, I meant <code>let res = try { ... };</code></p>



<a name="213985135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985135">(Oct 20 2020 at 22:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213981013">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213980931">said</a>:</p>
<blockquote>
<p>It would be nice specifically because then the two branches would be symmetrical, as opposed to with <code>match</code> or <code>if let</code></p>
</blockquote>
<p>I don't quite follow this bit</p>
</blockquote>
<p>For example, take this code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">do_stuff</span><span class="p">().</span><span class="n">is_err</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_other_stuff</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>This says:</p>
<ol>
<li>Try calling <code>do_stuff()</code></li>
<li>If it returns an error, call <code>do_other_stuff()</code></li>
</ol>
<p>But I would find this expressed more clearly as:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_stuff</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_other_stuff</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985181">(Oct 20 2020 at 22:02)</a>:</h4>
<p>Rather than writing <code>try { ... } else { ... }</code>, which raises questions about how to bind the error result in the else path, you could write <code>let result = try { ... }; if let Err(e) = result { ... }</code> which means the same thing.</p>



<a name="213985213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985213">(Oct 20 2020 at 22:02)</a>:</h4>
<p>Or if you don't want to bind <code>e</code>, you could write <code>if let Err(_) = result { ... }</code>.</p>



<a name="213985230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985230">(Oct 20 2020 at 22:02)</a>:</h4>
<p>Well in this case, I wouldn't even need that: I could just do <code>if let Err(_) = do_stuff() { ... }</code></p>



<a name="213985242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985242">(Oct 20 2020 at 22:02)</a>:</h4>
<p>We do currently have syntactic space reserved for this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span>: <span class="nc">keyword</span><span class="w"> </span><span class="err">`</span><span class="n">catch</span><span class="err">`</span><span class="w"> </span><span class="n">cannot</span><span class="w"> </span><span class="n">follow</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="err">`</span><span class="k">try</span><span class="err">`</span><span class="w"> </span><span class="n">block</span><span class="w"></span>
<span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">2</span>:<span class="mi">23</span><span class="w"></span>
<span class="w">  </span><span class="o">|</span><span class="w"></span>
<span class="mi">2</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="n">catch</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">  </span><span class="o">|</span><span class="w">                       </span><span class="o">^^^^^</span><span class="w"></span>
<span class="w">  </span><span class="o">|</span><span class="w"></span>
<span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">help</span>: <span class="nc">try</span><span class="w"> </span><span class="n">using</span><span class="w"> </span><span class="err">`</span><span class="k">match</span><span class="err">`</span><span class="w"> </span><span class="n">on</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="k">try</span><span class="err">`</span><span class="w"> </span><span class="n">block</span><span class="w"> </span><span class="n">instead</span><span class="w"></span>
</code></pre></div>

<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1cb59035e7410aa512774146ea30c8ae">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1cb59035e7410aa512774146ea30c8ae</a></p>



<a name="213985299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985299">(Oct 20 2020 at 22:03)</a>:</h4>
<p>I'm not saying this isn't possible to express currently, I just think it's clearer and simpler as <code>try { ... } else { ... }</code> or <code>try { ... } catch { ... }</code></p>



<a name="213985310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985310">(Oct 20 2020 at 22:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213985230">said</a>:</p>
<blockquote>
<p>Well in this case, I wouldn't even need that: I could just do <code>if let Err(_) = do_stuff() { ... }</code></p>
</blockquote>
<p>True, but I assumed part of the problem is that you want to do this when you don't have it all wrapped up in a function, and <code>if let Err(_) = { pile of code }  { other pile of code }</code> is really unclear.</p>



<a name="213985441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985441">(Oct 20 2020 at 22:05)</a>:</h4>
<p>I see :)</p>
<p>My specific use case is this, which is somewhere in between <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">).</span><span class="n">is_err</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="c1">// If we can't spawn a new thread, just run the test synchronously.</span>
<span class="w">                </span><span class="n">runtest</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985479">(Oct 20 2020 at 22:05)</a>:</h4>
<p>I would like to write that as:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// or `catch`, we could bikeshed :)</span>
<span class="w">    </span><span class="n">runtest</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985498">(Oct 20 2020 at 22:05)</a>:</h4>
<p>The problem is that an <code>else</code> (or <code>catch</code>) on <code>try</code> would only work for the "I don't care what error happened" case, unless we add binding syntax.</p>



<a name="213985503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985503">(Oct 20 2020 at 22:05)</a>:</h4>
<p>Going into my corrupted long-term memory, I think one of the big questions about this was that this would want to be able to handle different errors in some cases, so it arguably would need to support a whole chain of such blocks, with patterns on them.</p>



<a name="213985509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985509">(Oct 20 2020 at 22:05)</a>:</h4>
<p>And what it's competing against is <em>almost</em> as short.</p>



<a name="213985532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985532">(Oct 20 2020 at 22:06)</a>:</h4>
<p>I like the original <code>if</code> better TBH</p>



<a name="213985583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985583">(Oct 20 2020 at 22:06)</a>:</h4>
<p>fewer lines and it's clear what it's doing even if you've never used another language</p>



<a name="213985599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985599">(Oct 20 2020 at 22:06)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> <code>cfg.spawn(runtest) || runtest()</code> <span aria-label="smirk" class="emoji emoji-1f60f" role="img" title="smirk">:smirk:</span></p>



<a name="213985619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985619">(Oct 20 2020 at 22:06)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> Other than indentation conventions, why do you find:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// lots of code here</span>
<span class="p">};</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// handling code here</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>less clear than</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// lots of code here</span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// handling code here</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985621">(Oct 20 2020 at 22:06)</a>:</h4>
<p>The main reason I don't like the <code>if</code> is that it's easier to overlook side effects</p>



<a name="213985702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985702">(Oct 20 2020 at 22:08)</a>:</h4>
<p>Because the try-else version clearly shows the two branches, just like an if-else would if the condition didn't have side-effects</p>



<a name="213985745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985745">(Oct 20 2020 at 22:08)</a>:</h4>
<p>Hmm, come to think of it, if we had postfix <code>match</code> (like C# now does),</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// lots of code here</span>
<span class="p">}.</span><span class="k">match</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// handling code here</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="213985783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985783">(Oct 20 2020 at 22:08)</a>:</h4>
<p>I.e., I would prefer something like this, but that's not how the thread API works:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="n">can_spawn_thread</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// spawn thread</span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// do synchronously</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213985849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985849">(Oct 20 2020 at 22:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/213817-t-lang/topic/try.20else/near/213985745">said</a>:</p>
<blockquote>
<p>Hmm, come to think of it, if we had postfix <code>match</code> (like C# now does),</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// lots of code here</span>
<span class="p">}.</span><span class="k">match</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// handling code here</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>

</blockquote>
<p>Although that still wouldn't solve the thing that bothers me, which is that when the <code>try</code> code has side-effects, the <code>Err</code> case is treated separately</p>



<a name="213985883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985883">(Oct 20 2020 at 22:09)</a>:</h4>
<p>Can you elaborate on what you mean by "when the try code has side-effects"?</p>



<a name="213985957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985957">(Oct 20 2020 at 22:10)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> I think <span class="user-mention silent" data-user-id="307537">Camelid</span> means that <code>if cfg.spawn(...).is_err()</code> spawns a thread</p>



<a name="213985963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985963">(Oct 20 2020 at 22:10)</a>:</h4>
<p>personally I <em>like</em> that style</p>



<a name="213985985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985985">(Oct 20 2020 at 22:10)</a>:</h4>
<p>I like that style when the condition does not have side-effects, but in this case it does, which I find confusing.</p>



<a name="213985987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985987">(Oct 20 2020 at 22:10)</a>:</h4>
<p>it's like in C: <code>while ((c = getchar()) != EOF) { ... }</code></p>



<a name="213985997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213985997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213985997">(Oct 20 2020 at 22:11)</a>:</h4>
<p>Ah, I see.</p>



<a name="213986000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986000">(Oct 20 2020 at 22:11)</a>:</h4>
<p>which seems very idiomatic to me</p>



<a name="213986032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986032">(Oct 20 2020 at 22:11)</a>:</h4>
<p>I guess I prefer purely-functional code <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="213986067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986067">(Oct 20 2020 at 22:11)</a>:</h4>
<p>(even though my suggestion isn't magically purely-functional)</p>



<a name="213986206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986206">(Oct 20 2020 at 22:13)</a>:</h4>
<p>the other thing your syntax changes is now it swallows <em>everything</em> in the try block</p>



<a name="213986222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986222">(Oct 20 2020 at 22:13)</a>:</h4>
<p>previously it would only deal with errors from <code>spawn()</code>, now it could be any error that appears</p>



<a name="213986420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986420">(Oct 20 2020 at 22:15)</a>:</h4>
<p>What do you mean?</p>



<a name="213986511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213986511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213986511">(Oct 20 2020 at 22:16)</a>:</h4>
<p>I'm proposing that it would be purely syntactic sugar for calling <code>.is_err()</code>/<code>.is_none()</code></p>



<a name="213987405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213987405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213987405">(Oct 20 2020 at 22:26)</a>:</h4>
<p>In fact, my intention is sort of for try-else to not capture the error, at least for a first implementation.</p>



<a name="213992469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213992469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213992469">(Oct 20 2020 at 23:38)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">_try</span><span class="o">!</span><span class="p">({</span><span class="w"></span>
<span class="w">    </span><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">//fallback to a sequential run</span>
<span class="w">    </span><span class="n">runtest</span><span class="p">()</span><span class="w"></span>
<span class="p">})</span><span class="w"></span>
</code></pre></div>



<a name="213992676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/213992676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#213992676">(Oct 20 2020 at 23:41)</a>:</h4>
<p>(and for this case the simple:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">unwrap_or_else</span><span class="p">(</span><span class="o">|</span><span class="n">_</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">         </span><span class="c1">// fallback...</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>looks fairly idiomatic to me)</p>



<a name="214028771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214028771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214028771">(Oct 21 2020 at 09:45)</a>:</h4>
<p>Have y'all seen Swift's multi-pattern catch clauses? I think this is <em>fascinating</em></p>
<p><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0276-multi-pattern-catch-clauses.md">https://github.com/apple/swift-evolution/blob/master/proposals/0276-multi-pattern-catch-clauses.md</a></p>
<div class="codehilite" data-code-language="Swift"><pre><span></span><code><span class="k">do</span> <span class="p">{</span>
  <span class="k">try</span> <span class="n">performTask</span><span class="p">()</span>
<span class="p">}</span> <span class="k">catch</span> <span class="n">TaskError</span><span class="p">.</span><span class="n">someRecoverableError</span> <span class="p">{</span>    <span class="c1">// OK</span>
  <span class="n">recover</span><span class="p">()</span>
<span class="p">}</span> <span class="k">catch</span> <span class="n">TaskError</span><span class="p">.</span><span class="n">someFailure</span><span class="p">(</span><span class="kd">let</span> <span class="nv">msg</span><span class="p">),</span>
        <span class="n">TaskError</span><span class="p">.</span><span class="n">anotherFailure</span><span class="p">(</span><span class="kd">let</span> <span class="nv">msg</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Also Allowed</span>
  <span class="n">showMessage</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div>



<a name="214074996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214074996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214074996">(Oct 21 2020 at 16:17)</a>:</h4>
<p>Effectively a match?</p>



<a name="214079918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214079918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214079918">(Oct 21 2020 at 16:51)</a>:</h4>
<p>I think a more precise analogy would be <code>else if let Err(_)</code>, but I guess that is indeed effectively a match.</p>



<a name="214081514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214081514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yoshua Wuyts <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214081514">(Oct 21 2020 at 17:01)</a>:</h4>
<p>One interesting problem I haven't seen raised in this thread yet is how to match through a "carrier error type".</p>
<p>Say an <code>io::Error</code> was thrown, but we only wanted to recover from <code>io::ErrorKind:: Interrupted</code>, what would that look like? Could we make that convenient?</p>
<p>This gets rough for carriers which rely on downcasting to recover error kinds, and I wonder how much the language could do at that point to help.</p>



<a name="214087689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214087689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214087689">(Oct 21 2020 at 17:43)</a>:</h4>
<blockquote>
<p>Have y'all seen Swift's multi-pattern catch clauses?</p>
</blockquote>
<p>I think the rust version of </p>
<div class="codehilite" data-code-language="Swift"><pre><span></span><code><span class="k">catch</span> <span class="n">TaskError</span><span class="p">.</span><span class="n">someFailure</span><span class="p">(</span><span class="kd">let</span> <span class="nv">msg</span><span class="p">),</span>
      <span class="n">TaskError</span><span class="p">.</span><span class="n">anotherFailure</span><span class="p">(</span><span class="kd">let</span> <span class="nv">msg</span><span class="p">)</span>
</code></pre></div>

<p>would be</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">bikeshed</span><span class="w"> </span><span class="n">TaskError</span><span class="p">.</span><span class="n">someFailure</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"></span>
<span class="w">         </span><span class="n">TaskError</span><span class="p">.</span><span class="n">anotherFailure</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span><span class="w"></span>
</code></pre></div>

<p>like it is in a match.</p>



<a name="214091764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214091764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214091764">(Oct 21 2020 at 18:12)</a>:</h4>
<p>Something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">_try</span><span class="o">!</span><span class="p">({</span><span class="w"></span>
<span class="w">        </span><span class="n">try_run</span><span class="p">(</span><span class="n">task</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="n">catch</span><span class="w"> </span><span class="n">err</span>: <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">SomeRecoverableError</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">print</span><span class="o">!</span><span class="p">(</span><span class="s">"Recovered: "</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">task</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="n">SomeFailure</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"></span>
<span class="w">        </span><span class="n">AnotherFailure</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span>::<span class="n">log</span>::<span class="n">error</span><span class="o">!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">err</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=3868b1a6ef98de3dd0d8e6ea360e1f35">Playground</a></li>
</ul>



<a name="214093178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214093178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214093178">(Oct 21 2020 at 18:24)</a>:</h4>
<blockquote>
<p>Say an io::Error was thrown, but we only wanted to recover from io::ErrorKind:: Interrupted, what would that look like? Could we make that convenient?</p>
</blockquote>
<p>I feel like it'd probably be like <code>if</code> guards on <code>match</code> arms, for consistency.  Kinda like C#'s <code>catch (IoException ex) when (ex.Message.Foo())</code>.</p>



<a name="214110356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214110356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214110356">(Oct 21 2020 at 20:28)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">something_that_can_error</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">io</span>::<span class="n">Error</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&lt;</span><span class="n">bikeshed</span><span class="o">&gt;</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">&lt;</span><span class="n">bikeshed</span><span class="o">&gt;</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">io</span>::<span class="n">ErrorKind</span>::<span class="n">Interrupted</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">bar</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="214126565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214126565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214126565">(Oct 21 2020 at 23:14)</a>:</h4>
<p>I still like it the way it was in your PR (maybe with if-let chains)</p>



<a name="214126585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214126585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214126585">(Oct 21 2020 at 23:15)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">err</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">err</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">io</span>::<span class="n">ErrorKind</span>::<span class="n">WouldBlock</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</code></pre></div>



<a name="214127257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214127257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214127257">(Oct 21 2020 at 23:26)</a>:</h4>
<p>in general I'm biased to have as little syntax as possible; rust already has a reputation for being hard to learn</p>



<a name="214127685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214127685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214127685">(Oct 21 2020 at 23:32)</a>:</h4>
<p>The syntax I'm proposing looks fairly intuitive to me, but I guess maybe not to other people :)</p>



<a name="214127693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214127693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214127693">(Oct 21 2020 at 23:33)</a>:</h4>
<p>But having if-let chains implemented and stable would help a lot in this case</p>



<a name="214127714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20else/near/214127714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20else.html#214127714">(Oct 21 2020 at 23:33)</a>:</h4>
<p>Because then it could be:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">err</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cfg</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">runtest</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">err</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">io</span>::<span class="n">ErrorKind</span>::<span class="n">WouldBlock</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>